using System;
using System.Collections.Generic;
using PluginFrame;

namespace PcrDataStructure
{
    [Serializable]
    public class PcrDataFile : IPcrDataFile
    {
        [NonSerialized]
        protected IApplication mApplication;

        protected IPcrStructure mCurrentPcrStructure;

        protected string mDataFileFolder;

        protected string mProgramFileFolder;

        private string mPcrCfgFileFolder;

        protected string mReportFileFolder;

        protected DateTime mModificationTime;

        protected DateTime mCreateTime;

        protected string mFilePath;

        protected List<IPcrStructure> mPcrStructureList;

        protected bool mIsNewPcrFile;

        protected string mVersion;

        protected LanguageStrings mLanguage;

        [NonSerialized]
        protected bool mIsChanged;

        [NonSerialized]
        protected bool mIsLoadingAfterExperiment;

        public LanguageStrings Language
        {
            get
            {
                return mLanguage;
            }
            set
            {
                mLanguage = value;
            }
        }

        public bool IsLoadingAfterExperiment
        {
            get
            {
                return mIsLoadingAfterExperiment;
            }
            set
            {
                mIsLoadingAfterExperiment = value;
            }
        }

        public bool IsChanged
        {
            get
            {
                return mIsChanged;
            }
            set
            {
                mIsChanged = value;
            }
        }

        public IPcrStructure CurrentPcrStructure
        {
            get
            {
                return mCurrentPcrStructure;
            }
            set
            {
                mCurrentPcrStructure = value;
            }
        }

        public bool IsNewPcrFile
        {
            get
            {
                return mIsNewPcrFile;
            }
            set
            {
                mIsNewPcrFile = value;
            }
        }

        public List<IPcrStructure> PcrStructureList
        {
            get
            {
                return mPcrStructureList;
            }
        }

        DateTime IPcrDataFile.CreateTime
        {
            get
            {
                return mCreateTime;
            }
            set
            {
                mCreateTime = value;
            }
        }

        DateTime IPcrDataFile.ModificationTime
        {
            get
            {
                return mModificationTime;
            }
            set
            {
                mModificationTime = value;
            }
        }

        string IPcrDataFile.FilePath
        {
            get
            {
                return mFilePath;
            }
            set
            {
                mFilePath = value;
            }
        }

        string IPcrDataFile.DataFileFolder
        {
            get
            {
                return mDataFileFolder;
            }
            set
            {
                mDataFileFolder = value;
            }
        }

        string IPcrDataFile.ProjectFileFolder
        {
            get
            {
                return mProgramFileFolder;
            }
            set
            {
                mProgramFileFolder = value;
            }
        }

        string IPcrDataFile.PcrCfgFileFolder
        {
            get
            {
                return mPcrCfgFileFolder;
            }
            set
            {
                mPcrCfgFileFolder = value;
            }
        }

        string IPcrDataFile.ReportFileFolder
        {
            get
            {
                return mReportFileFolder;
            }
            set
            {
                mReportFileFolder = value;
            }
        }

        public IApplication Application
        {
            get
            {
                return mApplication;
            }
            set
            {
                mApplication = value;
            }
        }

        public string Version
        {
            get
            {
                return mVersion;
            }
            set
            {
                mVersion = value;
            }
        }

        public PcrDataFile(IApplication app)
        {
            mApplication = app;
            mPcrStructureList = new List<IPcrStructure>();
            IConfigurationService configurationService = (IConfigurationService)app.GetService("ConfigurationService");
            mVersion = configurationService.Version;
        }

        public void AddPcrStructure(IPcrStructure pcrStructure)
        {
            string text = "_" + mPcrStructureList.Count;
            foreach (IPcrExperiment pcrExperiment2 in pcrStructure.PcrExperiments)
            {
                IPcrExperiment pcrExperiment = FindExperiment(pcrExperiment2.Name);
                if (pcrExperiment != null)
                {
                    pcrExperiment2.Name += text;
                }
            }
            mPcrStructureList.Add(pcrStructure);
            pcrStructure.PcrDataFile = this;
        }

        public List<IPcrExperiment> GetPcrExperiments(bool isAvaibleExperiment)
        {
            List<IPcrExperiment> list = new List<IPcrExperiment>();
            foreach (IPcrStructure mPcrStructure in mPcrStructureList)
            {
                foreach (IPcrExperiment pcrExperiment in mPcrStructure.PcrExperiments)
                {
                    if (isAvaibleExperiment && !mIsNewPcrFile)
                    {
                        if (pcrExperiment.IsAvailable)
                        {
                            list.Add(pcrExperiment);
                        }
                    }
                    else
                    {
                        list.Add(pcrExperiment);
                    }
                }
            }
            return list;
        }

        public IPcrExperiment FindExperiment(string experimentName)
        {
            foreach (IPcrStructure mPcrStructure in mPcrStructureList)
            {
                foreach (IPcrExperiment pcrExperiment in mPcrStructure.PcrExperiments)
                {
                    if (experimentName.CompareTo(pcrExperiment.Name) == 0)
                    {
                        return pcrExperiment;
                    }
                }
            }
            return null;
        }

        public List<IPcrCell> GetAllPcrCellsInExperiment(string experimentName)
        {
            List<IPcrCell> result = null;
            IPcrExperiment pcrExperiment = FindExperiment(experimentName);
            if (pcrExperiment != null)
            {
                result = pcrExperiment.GetPcrCellsInExperiment();
            }
            return result;
        }

        public List<IPcrExperiment> GetExperimentsInSameGrid(IPcrExperiment originalExperiment)
        {
            List<IPcrExperiment> list = new List<IPcrExperiment>();
            list.Add(originalExperiment);
            List<IPcrExperiment> list2 = list;
            int indexInTab = originalExperiment.IndexInTab;
            foreach (IPcrStructure mPcrStructure in mPcrStructureList)
            {
                List<IPcrExperiment> pcrExperiments = mPcrStructure.PcrExperiments;
                foreach (IPcrExperiment item in pcrExperiments)
                {
                    if (item.IndexInTab != indexInTab && item.TabIndexs.Contains(indexInTab))
                    {
                        list2.Add(item);
                    }
                }
            }
            return list2;
        }
    }
}
